<?php
/**
 * Created by PhpStorm.
 * User: g
 * Date: 2018/1/18
 * Time: 17:55
 */

namespace app\index\controller;
use think\Db;
use think\Request;

class Workflowset extends Common
{
    private  $db;
    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        $this->db = Db::connect('db_config1');
    }

    public function index(){

        //查询工作流
        $where = [];
        $FLOW_NAME = $this->request->param('FLOWNAME');
        $this->assign('FLOWNAME',$FLOW_NAME);
        if($FLOW_NAME){
            $where['FLOWNAME'] = ['like',"%{$FLOW_NAME}%"];
        }
        $count = $this->db->table('tw_hz_flow_def')->where($where)->count();
        $data = $this->db->table('tw_hz_flow_def')->where($where)->paginate(10,$count);
        $page = $data->render();
        $data = $data->items();
        foreach($data as $k=>$v){
            $v['bdmc'] = db('cg_workflowset')->where('flow_id',$v['ID'])->value('table_brief');
            $data[$k] = $v;
        }
        $this->assign('page',$page);
        $this->assign('data',$data);
        return $this->fetch();
    }

    //流程设定
    public function set(){
        $id = $this->request->param('id');
        if($_POST){
            $data = [];
            $data['id'] = $this->request->post('table_id');
            $data['flow_id'] = $id;
            $bool = db('cg_workflowset')->update($data);
            if(false !== $bool){
                echo json_encode(['success'=>true,'msg'=>'设定成功']);
            }else{
                echo json_encode(['success'=>false,'msg'=>'设定失败']);
            }
            die;
        }
        $data = $this->db->table('tw_hz_flow_def')->where('ID',$id)->find();
        $this->assign('data',$data);
        //查询所有未绑定的工作流
        $where = ['flow_id'=>[['exp','is null or flow_id=0'],['exp',"= '".$id."'"],'or']];
        $workflow = db('cg_workflowset')->where($where)->select();
        $this->assign('workflow',$workflow);
//        echo db('cg_workflowset')->getLastSql();
        return $this->fetch();
    }
}